<?php

namespace app\index\model;

use app\common\entity\Config;
use app\common\entity\Log;
use app\common\entity\Mywallet;
use app\common\entity\MywalletLog;
use app\common\entity\Orders;
use app\common\entity\UserInviteCode;
use app\common\entity\UserProduct;
use app\common\entity\WithdrawRatio;
use app\common\service\Users\Cache;
use app\common\service\Users\Identity;
use app\common\service\Users\Service;
use think\Db;
use think\facade\Request;
use app\common\entity\Category;
class User {



    public function checkRegisterOpen() {
        $registerOpen = Config::getValue('register_open');
        if ($registerOpen) {
            return true;
        }
        return false;
    }

    public function checkIp() {
        $ipTotal = Config::getValue('register_ip');
        $ip = Request::ip();
        $total = \app\common\entity\User::where('register_ip', $ip)->count();
        if ($ipTotal > $total) {
            return true;
        }
        return false;
    }
    public function doRegister($data) {

        $entity = new \app\common\entity\User();
        $service = new Service();
        //邀请码
        if(isset($data['invite_code']) ){

            if ($data['invite_code'] == '0' ){
                $result = 0;
            }else{
                $result = UserInviteCode::getUserIdByCode($data['invite_code']);
                //$result = UserInviteCode::where('invite_code', $data['invite_code'])->value('user_id');
                if(!$result)return false;
            }
        }else{
            $result = 0;
        }

        $parentId = $result?$result:0;
        if( isset($data['mobile']) ){
            $entity->mobile = $data['mobile']??'';
            $entity->nick_name = $data['mobile'];
        }
        if(isset($data['nick_name'])){
            $entity->nick_name = $data['nick_name'];
        }
        if(isset($data['avatar'])){
            $entity->avatar = $data['avatar']??'/images/header.png';
        }else{
            $entity->avatar = '/images/header.png';
        }
        //$entity->avatar = '/img/head_img.png';
//        $entity->real_name = $data['real_name'];
//        $entity->zfb = $data['zfb'];
        if(isset($data['password'])){
            $entity->password = $service->getPassword($data['password']);
        }
        if(isset($data['safe_password'])){
            $entity->trad_password = $service->getPassword($data['safe_password']);
        }

        $entity->register_time = time();
        $entity->register_ip = Request::ip();
        $entity->login_ip = Request::ip();
        $entity->status = \app\common\entity\User::STATUS_DEFAULT;
        $entity->is_certification = \app\common\entity\User::AUTH_ERROR;
        if(isset($data['pid'])){
            $entity->pid = $data['pid'];
        }else{
            $entity->pid = $parentId;
        }
        if(isset($data['code'])){
            $entity->reg_code = $data['code'];
        }
        if(isset($data['openid'])){
            $entity->openid = $data['openid'];
        }

        if ($entity->save()) {
            // $nick_name = 'T'.$entity->id;
            // \app\common\entity\User::update(['nick_name' => $nick_name], ['id' => $entity->id]);
            
            $inviteCode = new UserInviteCode();
            $inviteCode->saveCode($entity->id);
            $my_wallet_id = Mywallet::insert(['user_id' =>$entity->id]);

            // MywalletLog::register($entity->pid);

            // \app\common\entity\User::upLevel($entity->pid);
//            $this->sendRegisterReward($entity);
            //增加邀请人数
            \app\common\entity\User::where('id', $parentId)->setInc('invite_count');
            // 奖励
            if($my_wallet_id){
                \app\common\shop\UserLevelModel::reg($entity->id,$entity->pid);
            }

            return $entity->id;
        }

        return false;
    }


   
    /**
     * 得到用户的详细信息
     */
    public function getInfo($id) {
        return \app\common\entity\User::where('id', $id)->find();
    }

    /**
     * 银行卡号 微信号 支付宝账号 唯一
     */
    public function checkMsg($type, $account, $id = '') {
        return \app\common\entity\User::where("$type", $account)->where('id', '<>', $id)->find();
    }

    public function doLogin($account, $password) {
        $user = \app\common\entity\User::where('mobile', $account)->find();
        if (!$user) {
            return '账号或者密码错误';
        }
        
        $model = new \app\common\service\Users\Service();

        if (!$model->checkPassword($password, $user)) {
            return '账号或者密码错误';
        }
        if ($user->is_close == -1) {
            return '此账号已被永久封号，无法自助解封。';
        }

        if ($user->status == \app\common\entity\User::STATUS_FORBIDDED) {
            return '此账号已被冻结，请联系平台客服。';
        }

        //保存session
        $identity = new Identity();
        $identity->saveSession($user);
        $login_time = session_id();

        \app\common\entity\User::where('login_time', $login_time)->update(['login_time'=>'']);
        \app\common\entity\User::where('mobile', $account)->update(['login_time'=>$login_time]);
        return true;
    }


    #获取区块链钱包地址
    function getaddress(&$arr){
        // $url = 'http://lll.weixqq4.top/web3/index/Newaccount';
        // $url = 'http://103.224.250.214/web3/index/Newaccount';
        
        $url = 'http://47.52.47.38/index/Newaccount';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $arr = curl_exec($ch); // 已经获取到内容，没有输出到页面上。
        curl_close($ch);

        $arr1 = substr($arr , 0 , 2);

        if($arr1 != '0x'){
            return $this->getaddress($arr);
        }else{
            return $arr;
        }

    }

}
